﻿using System;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI;
using YArchitech.Revit.Electric.Common;
using HYElectricCalculation.RoomIllumCalcAndLayout;

namespace HYElectricCalculation
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class ModelessFormApp : IExternalApplication
	{
		public Result OnShutdown(UIControlledApplication application)
		{
			if (ModelessFormApp.m_roomIllumCalcAndLayoutFrom != null && !ModelessFormApp.m_roomIllumCalcAndLayoutFrom.IsDisposed)
			{
				ModelessFormApp.m_roomIllumCalcAndLayoutFrom.Dispose();
				ModelessFormApp.m_roomIllumCalcAndLayoutFrom = null;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public Result OnStartup(UIControlledApplication application)
		{
			ModelessFormApp.m_roomIllumCalcAndLayoutFrom = null;
			ModelessFormApp.m_appThis = this;
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public void ShowForm(ExternalCommandData cmdData)
		{
			if (ModelessFormApp.m_roomIllumCalcAndLayoutFrom == null || ModelessFormApp.m_roomIllumCalcAndLayoutFrom.IsDisposed)
			{
				ModelessFromRequestHandler modelessFromRequestHandler = new ModelessFromRequestHandler();
				ExternalEvent exEvent = ExternalEvent.Create(modelessFromRequestHandler);
				ModelessFormApp.m_roomIllumCalcAndLayoutFrom = new RoomIllumCalcAndLayoutForm(cmdData, modelessFromRequestHandler, exEvent);
				ModelessFormApp.m_roomIllumCalcAndLayoutFrom.Show(WindowHelper.HWndRevit);
			}
		}

		internal static ModelessFormApp m_appThis;

		internal static RoomIllumCalcAndLayoutForm m_roomIllumCalcAndLayoutFrom;
	}
}
